﻿#include "EventApplicationWindow.hpp"
#include "EventResource.hpp"

#include <gtkmm/styleprovider.h>
#include <glibmm/signalproxy.h>
#include <iostream>

Event::ApplicationWindow::ApplicationWindow(BaseObjectType *cobject,
    const Glib::RefPtr<Gtk::Builder> &refBuilder)
    : Glib::ObjectBase("EventApplicationWindow"),
    Gtk::ApplicationWindow(cobject), m_refBuilder(refBuilder)
{
    /* 加载 CSS 样式表 */
    m_refCssProvider = Gtk::CssProvider::create();
    Gtk::StyleProvider::add_provider_for_display(
        get_display(), m_refCssProvider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    m_refCssProvider->signal_parsing_error().connect(
        [](const auto &section, const auto &error) { on_parsing_error(section, error); });
    gtk_css_provider_load_from_resource(m_refCssProvider->gobj(), "/org/korack/event/style.css");

    /* 绑定 UI 元素 */
    groups_box = Gtk::Builder::get_widget_derived<Event::EntryBox>(m_refBuilder, "groups_box");
    events_box = Gtk::Builder::get_widget_derived<Event::EntryBox>(m_refBuilder, "events_box");
    header_bar = m_refBuilder->get_widget<Gtk::HeaderBar>("header_bar");
    add_event_button = Gtk::Builder::get_widget_derived<Event::Button>(m_refBuilder, "add_event_button");

    header_bar->pack_end(*add_event_button);
}

Event::ApplicationWindow::~ApplicationWindow(void)
{
}

void Event::ApplicationWindow::on_parsing_error(
    const Glib::RefPtr<const Gtk::CssSection> &section, const Glib::Error &error)
{
    std::cerr << "on_parsing_error(): " << error.what() << std::endl;
    if (section)
    {
        const auto file = section->get_file();
        if (file)
        {
            std::cerr << "  URI = " << file->get_uri() << std::endl;
        }

        auto start_location = section->get_start_location();
        auto end_location = section->get_end_location();
        std::cerr << "  start_line = " << start_location.get_lines() + 1
            << ", end_line = " << end_location.get_lines() + 1 << std::endl;
        std::cerr << "  start_position = " << start_location.get_line_chars()
            << ", end_position = " << end_location.get_line_chars() << std::endl;
    }
}

Event::ApplicationWindow *
Event::ApplicationWindow::create()
{
    auto refBuilder = get_resource_from("/org/korack/event/ui/EventApplicationWindow.xml");
    auto window = Gtk::Builder::get_widget_derived<Event::ApplicationWindow>(
        refBuilder, "app_window");
    if (!window)
    {
        throw std::runtime_error("No object id=app_window in ui file.");
    }

    auto entry = Event::GroupEntry::create();
    window->groups_box->append(*entry);

    auto event = Event::EventEntry::create();
    event->set_can_modify(true);
    event->set_event_name("今日事件");
    event->set_event_describe("今日事件的描述");
    event->set_event_end_time("2024-12-10");
    event->set_group_name("Korack 的事件组");
    event->signal_clicked().connect([](int,double,double) { std::cout << "Clicked Frame.\n"; });
    window->append_event_entry(*event);

    return window;
}

void Event::ApplicationWindow::append_group_entry(Event::GroupEntry &entry)
{
    this->groups_box->append(entry);
}

void Event::ApplicationWindow::append_event_entry(Event::EventEntry &entry)
{
    this->events_box->append(entry);
}
